import React from 'react'
import { Button, Checkbox, Form, Input, message } from 'antd';
import { loginData } from '../apis/login'
import { useNavigate } from 'react-router-dom'


export default function Login() {
  const [a, b] = React.useState(false)
  const navigate = useNavigate()
  const onFinish = async (values) => {
    b(true)
    loginData(values).then(res => {
      console.log("res", res);
      
        localStorage.token = res.token
        localStorage.userInfo = JSON.stringify(res.userInfo)
        localStorage.permissions = JSON.stringify(res.permissions)
        message.success("登录成功")
        navigate("/nav")
    })
      .catch(err => {
        console.log("err", err);
      })
    // let res = await loginData(values)
    // console.log(res);
    // if (res.data.code == 1) {
    //   b(false)
    //   localStorage.token=res.data.data.token
    //   localStorage.userInfo=JSON.stringify(res.data.data.userInfo)
    //   localStorage.permissions=JSON.stringify(res.data.data.permissions)
    //   message.success("登录成功")
    //   navigate("/nav")
    // } else {
    //   b(false)
    //   message.error(res.data.msg||"登录失败")
    // }
  };
  const onFinishFailed = (errorInfo) => {
    console.log('Failed:', errorInfo);
  };

  return (
    <div className="login">
      <Form
        name="basic"
        labelCol={{
          span: 8,
        }}
        wrapperCol={{
          span: 16,
        }}
        style={{
          width: 400,
          color: 'white'
        }}
        onFinish={onFinish}
        onFinishFailed={onFinishFailed}
      >
        <Form.Item
          label={<div style={{ color: "#fff" }}>账号</div>}
          name="account"
          rules={[
            {
              required: true,
              message: '请输入账号',
            },
          ]}
        >
          <Input />
        </Form.Item>

        <Form.Item
          label={<div style={{ color: "#fff" }}>密码</div>}
          name="password"
          rules={[
            {
              required: true,
              message: '请输入密码',
            },
          ]}
        >
          <Input.Password />
        </Form.Item>


        <Form.Item
          wrapperCol={{
            offset: 8,
            span: 16,
          }}
        >
          <Button loading={a} type="primary" htmlType="submit">
            登录
          </Button>
        </Form.Item>
      </Form>

    </div>
  )
}
